package com.runachina;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import com.runachina.sqlite.MySqlite;

/**
 * Created by Isay on 2016/2/1.
 */
public class Test extends AndroidTestCase{

    /**
     *创建
     */
    public void test(){
        MySqlite mySqlite =new MySqlite(getContext(),1);//版本1
        SQLiteDatabase database = mySqlite.getReadableDatabase();
        mySqlite.getWritableDatabase();
    }

    /**
     * 增加
     */
    public void insert(){
        MySqlite mySqlite =new MySqlite(getContext(),1);
        SQLiteDatabase database = mySqlite.getReadableDatabase(); //此时创建数据库连接 database.close();

        ContentValues values=new ContentValues();
        values.put("name", "小李");
        values.put("age", "18");
        Long n=database.insert("rude",null,values);//成功：返回ID   失败：返回-1
    }

    /**
     * 删除
     */
    public void delete(){
        MySqlite mySqlite =new MySqlite(getContext(),1);
        SQLiteDatabase database = mySqlite.getReadableDatabase();

        int n =database.delete("rude","name=? and age=?",new String[]{"小王",21+""});//受到影响的行数：删除失败就是0

        database.close();
       }


    /**
     * 修改
     */
    public void update(){
        MySqlite mySqlite =new MySqlite(getContext(),1);
        SQLiteDatabase database = mySqlite.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put("name", "小红");//修改为小红
        /**
         * 返回：受到影响的行数：没有就是0
         * 表名：rude
         * 修改为：values
         * 修改哪一列： name=? ,null表示所有
         * 哪一列匹配的什么值：new String[]{"小李"},null表示所有
         */
        int n = database.update("rude",values,"name=?",new String[]{"小李"});
        database.close();
    }

    /**
     * 查询
     */
    public void select(){
        MySqlite mySqlite =new MySqlite(getContext(),1);
        SQLiteDatabase database = mySqlite.getReadableDatabase();
        /**
         * 1>>true 剔除重复数据，false不剔除
         * 2>>表名
         * 3>>查询列
         * 4>>筛选条件规则
         * 5>>指定筛选条件参数值
         * 6>>groupBy分组
         * 7>>having 分组筛选
         * 8>>orderBy 排序  如：升序"age asc"
         * 9>>limit 分页
         */
        Cursor cursor = database.query(true, "rude", new String[]{"id", "name", "age"}, null, null, null, null, null, null);
        while(cursor.moveToNext()){
            int id =cursor.getInt(0);//或者=cursor.getColumnIndex("id")
            String name=cursor.getString(1);
            int age =cursor.getInt(2);
        }
        database.close();//关闭连接
    }
}
